﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace XtyWallpaper
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args != null && args.Length > 0 && args[0] == "-uninst")
            {
                AppUpdate update = new AppUpdate();
                update.Uninst();
                if (MessageBox.Show("卸载成功！", "XtyWallpaper 卸载", MessageBoxButtons.OK) == DialogResult.OK)
                {
                    Application.Exit();//关闭程序
                }
            }
            else
            {
                Console.WriteLine("star....");
                System.Timers.Timer timersTimer = new System.Timers.Timer();
                timersTimer.Enabled = true;
                timersTimer.Interval = 10000;//程序 10 秒后。马上执行
                timersTimer.Elapsed += new System.Timers.ElapsedEventHandler(timersTimer_Elapsed);

                Application.Run();
            }
        }

        static void timersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Console.WriteLine("Elapsed run .....");
            System.Timers.Timer timersTimer = (System.Timers.Timer)sender;

            DateTime now = DateTime.Now;
            TimeSpan span = new TimeSpan(1, 0, 0);

            //计算下一次执行的时间间隔
            if (now.Hour > 0 && now.Hour <= 12)
            {
                span = (new DateTime(now.Year, now.Month, now.Day, 12, 10, 0)) - now;
            }
            else if (now.Hour > 12 && now.Hour <= 17)
            {
                span = (new DateTime(now.Year, now.Month, now.Day, 17, 10, 0)) - now;
            }
            else if (now.Hour > 17 && now.Hour <= 23)
            {
                span = (new DateTime(now.Year, now.Month, now.Day, 23, 59, 59)).AddMinutes(10) - now;
            }

            int interval = Convert.ToInt32(span.TotalMilliseconds);
            Console.WriteLine("Elapsed Interval:" + interval);
            timersTimer.Interval = interval;// 默认 360000 = 一个小时执行一次

            AppUpdate paper = new AppUpdate();
            paper.Init();
            paper.Update();//更新

            paper = null;

            GC.Collect();
        }
    }
}
